package com.springcloud.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.springcloud.common.constant.RedisConstant;
import com.springcloud.common.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * @ClassName TokenFilter
 * @Author XuWeiZhi
 * @Description 权限拦截 区分买家和买家的权限
 * @Date 2018-11-07 20:42 星期三 MySpringCloud
 * @VERSION 1.0.0
 **/
@Component
public class AuthFilter extends ZuulFilter {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 返回拦截类型 zuul处理之前拦截过滤
     */
    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    /**
     * 拦截顺序，越小的越靠前
     */
    @Override
    public int filterOrder() {
        return PRE_DECORATION_FILTER_ORDER - 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    /*
      实现的逻辑
     */
    @Override
    public Object run() throws ZuulException {
        /*获取当前对象的上下文*/
        //RequestContext currentContext = RequestContext.getCurrentContext();
        //HttpServletRequest request = currentContext.getRequest();
        //
        ///*
        //  /order/create 只能买家访问(Cookie有openid)
        //  /order/finish 只能卖家访问(Cookie有token,并对应的redis)
        //  /product/list 都可  访问
        // */
        //if ("/order/order/create".equals(request.getRequestURI())) {
        //    Cookie cookie = CookieUtils.get(request, "openid");
        //    if (cookie == null || StringUtils.isEmpty(cookie.getValue())) {
        //        currentContext.setSendZuulResponse(false);
        //        currentContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
        //    }
        //}
        //
        //if("/order/order/finish".equals(request.getRequestURI())){
        //    Cookie cookie = CookieUtils.get(request,"token");
        //    if(cookie == null || StringUtils.isEmpty(cookie.getValue())||
        //       StringUtils.isEmpty(redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_TEMPLATE,cookie.getValue())))){
        //        currentContext.setSendZuulResponse(false);
        //        currentContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
        //    }
        //}
        return null;
    }

}
